package com.cmft.generator;

import com.cmft.generator.enums.TemplateType;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;

import static com.cmft.generator.GeneratorConstant.*;

/**
 * Velocity加载器
 */
public class VelocityLoader {
    private static final String DEFAULT_CHARSET = "UTF-8";
    private static final String DOUBLELINE = "\\\\";
    private static final String DOT = "\\.";
    private static final String JAVA_EXTEND_FILE_NAME = ".java";


    public static void generatorCode(VelocityContext ctx, TemplateType templateType) {
        String fileContent = getStringFromTemplate(ctx, templateType);
        System.out.println(fileContent);
        String filePath = getFilePath(ctx, templateType);
        System.out.println(filePath);
        createFile(filePath, fileContent);
    }

    public static String getStringFromTemplate(VelocityContext ctx, TemplateType templateType) {
        VelocityEngine ve = new VelocityEngine();
        ve.setProperty(Velocity.ENCODING_DEFAULT, DEFAULT_CHARSET);
        ve.setProperty(Velocity.INPUT_ENCODING, DEFAULT_CHARSET);
        ve.setProperty(Velocity.OUTPUT_ENCODING, DEFAULT_CHARSET);
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        ve.init();

        // 获取模板文件
        Template t = ve.getTemplate(templateType.getCode() + ".vm");

        StringWriter sw = new StringWriter();
        t.merge(ctx, sw);
        return sw.toString();
    }

    public static void createFile(String filePath, String fileContent) {
        try {
            FileUtils.write(new File(filePath), fileContent, DEFAULT_CHARSET);
        } catch (IOException e) {
            throw new RuntimeException("写文件失败" + filePath, e);
        }
    }


    public static String getFilePath(VelocityContext ctx, TemplateType templateType) {
        String mainDir = ObjectUtils.toString(ctx.get(MAINDIR));
        String strPackage = ObjectUtils.toString(ctx.get(VPACKAGE));
        String entityName = ObjectUtils.toString(ctx.get(VENTITYNAME));
        String entityNameExtend = ObjectUtils.toString(ctx.get(VENTITYNAMEEXTEND));


        String packageDir = strPackage.replaceAll(DOT, DOUBLELINE);
        String fileName = getFileName(entityName, templateType);

        if (templateType == TemplateType.ENTITY && StringUtils.isNotEmpty(entityNameExtend)) {
            fileName = fileName + entityNameExtend;
        }
        fileName = fileName + JAVA_EXTEND_FILE_NAME;

        System.out.println(fileName);
        StringBuilder pathBuilder = new StringBuilder();
        pathBuilder.append(mainDir).append(DOUBLELINE)//
                .append(packageDir).append(DOUBLELINE)//
                .append(templateType.getCode()).append(DOUBLELINE)//
                .append(fileName);
        return pathBuilder.toString();
    }

    public static String getFileName(String entityName, TemplateType templateType) {
        if (templateType == TemplateType.ENTITY) {
            return entityName;
        }
        String value = templateType.getCode();
        return entityName + value.substring(0, 1).toUpperCase() + value.substring(1);
    }
}
